package com.guigu.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.R;
import com.guigu.eduservice.dto.Request.DetectionReportImageRequest;
import com.guigu.eduservice.dto.Request.InspectionItem;
import com.guigu.eduservice.dto.Request.MediaItem;
import com.guigu.eduservice.entity.DetectionReport;
import com.guigu.eduservice.entity.DetectionReportImage;
import com.guigu.eduservice.entity.Equipment;
import com.guigu.eduservice.service.DetectionReportImageService;
import com.guigu.eduservice.service.DetectionReportService;
import com.guigu.eduservice.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 检测报告图片表（存储检测过程中的证据图片） 前端控制器
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/eduservice/detectionReportImage")
public class DetectionReportImageController {
    @Autowired
    private DetectionReportImageService detectionReportImageService;
    @Autowired
    private DetectionReportService detectionReportService;
    @Autowired
    private EquipmentService equipmentService;
    //新建检测报告图片表(检测报告图片表,检测报告图片表,设备表 )//不能用，要 Items必须为list，并非键值对
    @PostMapping("/addDetectionReportImage")
    public R addDetectionReportImage( @RequestBody DetectionReportImageRequest detectionReportImages){
        //三个检测项
        int size = detectionReportImages.getItems().size();
        Long extendsReportId = detectionReportImages.getExtendsReportId(); //任务id
        boolean save=false; //结果返回的状态
        int sort=0; //图片排序
        int resultQuantity=0;//异常数量
        for (int i = 0; i < size; i++){
            // 将Map的Value转换为List
            List<InspectionItem> valueList = new ArrayList<>(detectionReportImages.getItems().values());
            //单个检查项
            InspectionItem inspectionItem = valueList.get(i);
            //表单数据
            String imageDesc=inspectionItem.getResult()+','+inspectionItem.getRemark();//也是检测报告的
            //修改检测报告表
            DetectionReport detectionReport = new DetectionReport();
            detectionReport.setReportId(extendsReportId);
            if(i==0){
                //外观检测结果
                detectionReport.setAppearanceDetection(imageDesc);
            }
            if(i==1){
                //部件功能检测结果
                detectionReport.setComponentFunctionDetection(imageDesc);
            }
            if(i==2){
                //性能参数检测结果
                detectionReport.setPerformanceParameterDetection(imageDesc);
            }
            //判断检测结果异常数
            if(Objects.equals(inspectionItem.getResult(),"abnormal")){
                resultQuantity=resultQuantity+1;
            }
            //检测结果异常数大于等于2
            if (resultQuantity>=2){
                detectionReport.setDetectionConclusion("检测结果异常");
            }else {
                detectionReport.setDetectionConclusion("检测结果正常");
            }
            //审核
            detectionReport.setAuditStatus(1);
            LocalDateTime now = LocalDateTime.now();
            detectionReport.setAuditTime(now);
            detectionReport.setAuditRemark("审核完成");
            save=detectionReportService.updateById(detectionReport);
            int imagesSize = inspectionItem.getMedia().size();
            for(int j = 0; j < imagesSize; j++){
                //单个图片
                MediaItem mediaItem = inspectionItem.getMedia().get(j);
                //填写检测报告图片表
                DetectionReportImage detectionReportImage = new DetectionReportImage();
                detectionReportImage.setReportId(extendsReportId);
                detectionReportImage.setImageUrl(mediaItem.getUrl());
                detectionReportImage.setImageDesc(imageDesc);
                sort=sort+1;
                detectionReportImage.setSort(sort);
                save = detectionReportImageService.save(detectionReportImage);
            }
        }
        if (save) {
            //修改设备表的is_detected和detection_report_id
            DetectionReport byId = detectionReportService.getById(extendsReportId);
            Long equipmentId = byId.getEquipmentId();
            Equipment equipment = equipmentService.getById(equipmentId);
            equipment.setIsDetected(1);
            equipment.setDetectionReportId(extendsReportId);
            equipmentService.updateById(equipment);
            return R.ok().message("新建检测报告图片成功");
        }else {
            return R.error().message("新建检测报告图片失败");
        }

    }
    //查询检测报告图片表
    @GetMapping("/getDetectionReportImage/{reportId}")
    public R getDetectionReportImage( @PathVariable int reportId){
        QueryWrapper<DetectionReportImage> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("report_id",reportId);
        List<DetectionReportImage> list = detectionReportImageService.list(QueryWrapper);
        return R.ok().data("list",list);
    }
}
